﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace MGFramework
{
    public enum UIAnimType
    {
        NONE,//无动画
        SUPERIMPOSED,//无动画并叠加在现有页面上
        LEFTTORIGHT,//从左向右
        RIGHTTOLEFT,//从右向左
        MINTOMAX,//由小变大
        ALPHA,//逐渐出现
    }

    public class UICenter : MonoBehaviour
    {
        public static UICenter instance { private set; get; }
        private Dictionary<string, PanelBase> panel_dict = new Dictionary<string, PanelBase>();
        private List<string> BrowsingRecord = new List<string>();
        private bool isRecordBrowsing = false;
        protected string last_panel;
        protected string current_panel;

        private float screen_width;
        void Awake()
        {
            instance = this;
            screen_width = Screen.width + 4;
        }

        /// <summary>
        /// 注册页面
        /// </summary>
        /// <param name="name">页面名字</param>
        /// <param name="panel">页面</param>
        public void RegisterPanel(string name, PanelBase panel)
        {
            if (panel_dict.ContainsKey(name))
            {
                panel_dict[name] = panel;
            }
            else
            {
                if (current_panel == null) current_panel = name;
                panel_dict.Add(name, panel);
            }
        }

        /// <summary>
        /// 注销页面
        /// </summary>
        /// <param name="name">页面名字</param>
        public void LogoutPanel(string name)
        {
            if (panel_dict.ContainsKey(name))
            {
                panel_dict.Remove(name);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="method">方法id</param>
        public void CallPanelMethod(string name, int method, params object[] data)
        {
            if (panel_dict.ContainsKey(name))
            {
                panel_dict[name].CallPanelMethod(method, data);
            }
        }

        /// <summary>
        /// 打开页面
        /// </summary>
        /// <param name="panel_name">页面名字</param>
        /// <param name="anim">动画</param>
        public void OpenPanel(string panel_name, UIAnimType anim = UIAnimType.LEFTTORIGHT)
        {
            PanelBase temp_next = null;
            PanelBase temp_current = panel_dict[current_panel];
            if (panel_name == current_panel) return;
            if (panel_dict.ContainsKey(panel_name))
            {
                temp_next = panel_dict[panel_name];
            }
            else
            {
                Transform temp = transform.Find(panel_name);
                if (temp == null)
                {
                    temp = (Instantiate(Resources.Load("UIPanel/" + panel_name), transform) as GameObject).transform;
                }
                temp_next = temp.GetComponent<PanelBase>();
                if (temp_next != null)
                    temp_next.UICenterInitPanel();
                else return;
            }
            switch (anim)
            {
                case UIAnimType.NONE:
                    NoneAnim(temp_current, temp_next);
                    break;
                case UIAnimType.SUPERIMPOSED:
                    Superimposed(temp_current, temp_next);
                    break;
                case UIAnimType.LEFTTORIGHT:
                    LeftToRight(temp_current, temp_next);
                    break;
                case UIAnimType.RIGHTTOLEFT:
                    RightToLetf(temp_current, temp_next);
                    break;
                case UIAnimType.MINTOMAX:
                    MinToMax(temp_current, temp_next);
                    break;
                case UIAnimType.ALPHA:
                    AlphaShow(temp_current, temp_next);
                    break;
            }
        }

        /// <summary>
        /// 返回之前页面
        /// </summary>
        public bool ReturnPrevPanel(UIAnimType anim = UIAnimType.LEFTTORIGHT)
        {
            if (last_panel == null) return false;
            PanelBase temp_next = null;
            PanelBase temp_current = panel_dict[current_panel];
            if (panel_dict.ContainsKey(last_panel))
            {
                temp_next = panel_dict[last_panel];
            }
            else
            {
                Transform temp = transform.Find(last_panel);
                if (temp == null)
                {
                    temp = (Instantiate(Resources.Load("UIPanel/" + last_panel), transform) as GameObject).transform;
                }
                temp_next = temp.GetComponent<PanelBase>();
                if (temp_next != null) panel_dict.Add(last_panel, temp_next);
                else return false;
            }
            switch (anim)
            {
                case UIAnimType.NONE:
                    NoneAnim(temp_current, temp_next);
                    break;
                case UIAnimType.SUPERIMPOSED:
                    Superimposed(temp_current, temp_next);
                    break;
                case UIAnimType.LEFTTORIGHT:
                    LeftToRight(temp_current, temp_next);
                    break;
                case UIAnimType.RIGHTTOLEFT:
                    RightToLetf(temp_current, temp_next);
                    break;
                case UIAnimType.MINTOMAX:
                    MinToMax(temp_current, temp_next);
                    break;
            }
            last_panel = null;
            return true;
        }

        /// <summary>
        /// 清空浏览记录
        /// </summary>
        public void ClearBrowsingRecord()
        {
            BrowsingRecord.Clear();
        }

        /// <summary>
        /// 无动画
        /// </summary>
        /// <param name="current">当前页面</param>
        /// <param name="next">下个页面</param>
        private void NoneAnim(PanelBase current, PanelBase next)
        {
            current.gameObject.SetActive(false);
            next.transform.localPosition = Vector3.zero;
            next.gameObject.SetActive(true);
            if (isRecordBrowsing) BrowsingRecord.Add(current_panel);
            last_panel = current_panel;
            current_panel = next.name;
            next.OpenClick();
        }

        /// <summary>
        /// next界面盖在当前界面上
        /// </summary>
        /// <param name="current"></param>
        /// <param name="next"></param>
        private void Superimposed(PanelBase current, PanelBase next)
        {
            next.gameObject.SetActive(true);
            next.transform.localPosition = Vector3.zero;
            if (isRecordBrowsing) BrowsingRecord.Add(current_panel);
            last_panel = current_panel;
            current_panel = next.name;
            next.OpenClick();
        }

        /// <summary>
        /// 从左向右
        /// </summary>
        /// <param name="current"></param>
        /// <param name="next"></param>
        private void LeftToRight(PanelBase current, PanelBase next)
        {
            current.CloseClick();
            next.CloseClick();
            next.transform.localPosition = new Vector3(-screen_width, 0, 0);
            next.gameObject.SetActive(true);
            next.transform.DOLocalMoveX(0, 0.3f).OnComplete(() => {
                current.transform.DOKill();
                current.transform.localPosition = Vector3.zero;
                current.gameObject.SetActive(false);
                next.OpenClick();
            });
            current.transform.DOLocalMoveX(screen_width, 0.3f);
            if (isRecordBrowsing) BrowsingRecord.Add(current_panel);
            last_panel = current_panel;
            current_panel = next.name;
        }

        /// <summary>
        /// 从右向左
        /// </summary>
        /// <param name="current"></param>
        /// <param name="next"></param>
        private void RightToLetf(PanelBase current, PanelBase next)
        {
            current.CloseClick();
            next.CloseClick();
            next.transform.localPosition = new Vector3(screen_width, 0, 0);
            next.gameObject.SetActive(true);
            next.transform.DOLocalMoveX(0, 0.3f).OnComplete(() => {
                current.transform.DOKill();
                current.transform.localPosition = Vector3.zero;
                current.gameObject.SetActive(false);
                next.OpenClick();
            });
            current.transform.DOLocalMoveX(-screen_width, 0.3f);
            if (isRecordBrowsing) BrowsingRecord.Add(current_panel);
            last_panel = current_panel;
            current_panel = next.name;
        }

        /// <summary>
        /// 从小变大
        /// </summary>
        /// <param name="current"></param>
        /// <param name="next"></param>
        private void MinToMax(PanelBase current, PanelBase next)
        {
            current.CloseClick();
            next.CloseClick();
            next.transform.localScale = Vector3.zero;
            next.gameObject.SetActive(true);
            next.transform.DOScale(new Vector3(1, 1, 1), 0.3f).OnComplete(() => {
                current.transform.DOKill();
                current.transform.localScale = new Vector3(1, 1, 1);
                current.gameObject.SetActive(false);
                current.transform.DOKill();
                next.OpenClick();
            });
            current.transform.DOScale(Vector3.zero, 0.3f);
            if (isRecordBrowsing) BrowsingRecord.Add(current_panel);
            last_panel = current_panel;
            current_panel = next.name;
        }

        /// <summary>
        /// 逐渐出现（需要为panel添加Cavas Group）
        /// </summary>
        /// <param name="current"></param>
        /// <param name="next"></param>
        private void AlphaShow(PanelBase current, PanelBase next)
        {
            current.CloseClick();
            next.CloseClick();
            if (next.GetCanvasGroup() == null || current.GetCanvasGroup() == null)
            {
                NoneAnim(current, next);
                return;
            }
            next.GetCanvasGroup().alpha = 0;
            next.transform.localScale = new Vector3(1, 1, 1);
            next.gameObject.SetActive(true);
            current.GetCanvasGroup().DOFade(0, 0.3f).OnComplete(() => {
                current.transform.DOKill();
                current.gameObject.SetActive(false);
                current.GetCanvasGroup().alpha = 1;
                current.transform.DOKill();
                next.OpenClick();
            });
            next.GetCanvasGroup().DOFade(1, 0.3f);
            if (isRecordBrowsing) BrowsingRecord.Add(current_panel);
            last_panel = current_panel;
            current_panel = next.name;
        }
    }
}